<?php
declare (strict_types=1);

namespace app\common\model;

use app\common\tool\Sms;

class UserCode extends Base
{
    /**
     * 获取短信验证码
     */
    public static function sendMsgCode($mobile, $action)
    {
//        try {
//            $time = time();
//            //60秒内只能获取一次验证码
//            $has = parent::where(['mobile' => $mobile, 'action' => $action, 'is_del' => 0])
//                ->where('create_time', '>=', $time - 60)->find();
//            if ($has) {
//                throw new \Exception('60秒内只能发送一次');
//            }
//            $code = rand(100000, 999999);
//            $res = Sms::Send($mobile, $code);
//            if ($res !== true) {
//                throw new \Exception($res);
//            }
//            $end_time = $time + 300;
//            $data = [
//                'mobile' => $mobile,
//                'code' => $code,
//                'action' => $action,
//                'end_time' => $end_time,
//                'create_time' => $time,
//                'update_time' => $time
//            ];
//            parent::create($data);
//
//        } catch (\Exception $e) {
//
//            return ['code' => -1, 'msg' => $e->getMessage()];
//        }

        return ['code' => 1, 'msg' => '已发送'];
    }

    /**
     * 校验短信验证码
     */
    public static function verifyMsgCode($mobile, $code, $action)
    {
//        try {
//            $time = time();
//            $has = parent::where(['mobile' => $mobile, 'code' => $code, 'action' => $action, 'is_del' => 0])
//                ->where('end_time', '>=', $time)->find();
//            if (!$has) {
//                throw new \Exception('验证码无效');
//            }
//            $has->delete();
//
//        } catch (\Exception $e) {
//
//            return $e->getMessage();
//        }

        return true;
    }
}